<template>
  <el-dialog title="发票信息" v-model="visible" width="600px" :close-on-click-modal="false" :show-close="false" :close-on-press-escape="false">
    <el-form class="invoiveForm" ref="invoiveRef" :label-width="100" label-position="left" :model="voviceData.form.vovice" :show-message="false">
      <el-form-item label="发票类型">
        <div class="type">
          <div class="invoicetype" :class="{ activeinvoicetype: voviceData.form.vovice.invoiceType == 'NORMAL' }" @click="voviceData.form.vovice.invoiceType = 'NORMAL'">
            普通发票
            <img v-if="voviceData.form.vovice.invoiceType == 'NORMAL'" class="triangle" src="@/assets/images/gou.svg" />
          </div>
          <div class="invoicetype" :class="{ activeinvoicetype: voviceData.form.vovice.invoiceType === 'ADDEDTAX' }" @click="voviceData.form.vovice.invoiceType = 'ADDEDTAX'">
            增值税发票
            <img v-if="voviceData.form.vovice.invoiceType === 'ADDEDTAX'" class="triangle" src="@/assets/images/gou.svg" />
          </div>
        </div>
      </el-form-item>
      <el-form-item label="发票内容">
        <div class="invocontent">明细</div>
      </el-form-item>
      <el-form-item label="发票抬头" prop="invoiceTitle" required>
        <el-input v-model="voviceData.form.vovice.invoiceTitle" class="site-input"></el-input>
      </el-form-item>
      <el-form-item label="发票识别号" prop="taxpayerId" required>
        <el-input v-model="voviceData.form.vovice.taxpayerId" class="site-input"></el-input>
      </el-form-item>

      <el-form-item class="btns">
        <Button class="is-plain" label="取消" @click="onCancelClick" />
        <Button type="warning" label="确认" @click="onSubmitClick" />
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script setup>
import { reactive, computed, ref, toRefs } from 'vue'
import api from '@/api'
import { ElMessage } from 'element-plus'
import Button from '@/components/button/button.vue'

const props = defineProps(['visible', 'vovice'])
const emit = defineEmits(['update:visible', 'change'])

const visible = computed({
  get: () => {
    return props.visible
  },
  set: (value) => emit('update:visible', value)
})

const { vovice } = toRefs(props)
const voviceData = reactive({ form: { vovice } })
const invoiveRef = ref()

const save = async () => {
  let { err } = await api.post('/medinstrument/site/customer/updateInvoice', voviceData.form.vovice)
  if (err) {
    ElMessage.error(err)
  } else {
    ElMessage.success('修改开票信息成功')
    emit('change')
    visible.value = false
  }
}

const onCancelClick = () => {
  visible.value = false
}

const onSubmitClick = () => {
  invoiveRef.value.validate((valid) => {
    if (valid) {
      save()
    }
  })
}
</script>

<style scoped lang="scss">
.invoiveForm {
  padding: 0 30px 30px 30px;

  .site-input {
    width: 410px;
  }

  .type {
    display: flex;

    .invoicetype {
      padding: 10px 12px;
      border: 1px solid #ccc;
      margin-right: 20px;
      text-align: center;
      line-height: 20px;
      position: relative;
      cursor: pointer;

      .triangle {
        position: absolute;
        bottom: 0;
        right: 0;
      }
    }

    .activeinvoicetype {
      border: 1px solid var(--color-primary);
    }

    .invocontent {
      width: 52px;
      height: 40px;
      border: 1px solid #ccc;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}
</style>
